/**
 * Created by yaohuan on 2018/4/11 0011.
 */
define(function () {

    var self;

    var fwzp = app.Module.extend({

        init: function () {
            self = this;
            self.productData = [];
            self.mData = {
                tabType: ['3', '4',''],
                orderSet: {}
            };
            self.goodsList = [];
            self.searchData = {
                date: '',
                type: '2'
            };
            self.currentIndex = 0;
            self._super({
                _tag: 'p_fwzp', // key, 对象放进pageModule
                _sliderId: 'slider', // tabs
                _refreshAutoInit: true //自动加载刷新控件
            }, win);

            mui.ready(function () {
                Vue.component('fwzp-listitem', {
                    template: app.getByid(doc, 'item-template'),
                    props: {
                        model: Array
                    },
                    methods: {
                        showDetail: function (item) {
                            self.productData.removeAll();
                            self.productData.push(item);
                            app.openPage({
                                src: "fwzp/fwpg-info.html",
                                argsKey: 'fwzp-info',
                                args: {
                                    items: self.productData,
                                    cb: function () {
                                        //刷新数据
                                        self._refresh();
                                    }
                                }
                            });
                        },
                        changed:function (value) {
                            var goods = self.goodsList;
                            var count = 0;
                            goods.forEach(function (v) {
                                if(v.checked == value && value ==true){
                                    count ++;
                                }
                            });
                            if(count == goods.length){
                                if(value){
                                    $('#jjsq input[type=checkbox]').prop('checked', value);

                                }else {
                                    $('#jjsq input[type=checkbox]').prop('checked', !value);
                                }
                            }else{
                                if(value){
                                    $('#jjsq input[type=checkbox]').prop('checked', !value);
                                }else {
                                    $('#jjsq input[type=checkbox]').prop('checked', value);
                                }
                            }
                        }
                    }
                });

                new Vue({
                    el: doc.getElementById('searchs'),
                    data: {
                        md: self.searchData
                    },
                    watch:{
                        'md.date':function () {
                            self.goodsList.removeAll();
                            self._refresh();
                        }
                    },
                    methods: {
                        today: function () {
                            self.searchData.date = new Date().Format('yyyy-MM-dd');
                            self.searchData.type = 0;
                            self._refresh();
                        },
                        tomorrow: function () {
                            self.searchData.type = 1;
                            self.searchData.date = new Date().addDays(1, 'yyyy-MM-dd');
                            self._refresh();
                        },
                        all: function () {
                            self.searchData.date = '';
                            self.searchData.type = 2;
                            self._refresh();
                        }
                    }
                });
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll1', 'scroll2'];
            }
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/fwzp/getServiceOrderListData",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    MMRD020: self.mData.tabType[self.currentIndex],
                    MMRD004: self.searchData.date
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        for (var i = 0; i < result.data.data.length; i++) {
                            result.data.data[i].checked == false;
                        }
                        self.goodsList = data.data;
                        _super({
                            obj: goods,
                            data:data.data,
                            dataLen: data.data.length,
                            nodata: self._refreshIndex == 0 ? '无派工服务单...' : '无报工服务单...'
                        });
                    }
                }
            });
        },
        _lazy:function () {
            this._super();

            doc.getElementById('slider').addEventListener('slide', function (e) {
                self.currentIndex = e.detail.slideNumber;
                self.goodsList.removeAll();
                self._refresh();
            });

            $('#allCheck').click(function () {
                var _type = self.mData.tabType[self.currentIndex]-2;
                var goods = self.goodsList;
                if ($('#allCheck').is(':checked')) {
                    $('#scroll' + _type + ' input[type=checkbox]').prop('checked', true);
                    for (var i = 0; i < goods.length; i++) {
                        goods[i].checked = true;
                    }
                } else {
                    $('#scroll' + _type + ' input[type=checkbox]').prop('checked', false);
                    for (var i = 0; i < goods.length; i++) {
                        goods[i].checked = false;
                    }
                }
            });

            $('#btn_qbpg').click(function () {
                self.productData.removeAll();
                var goods = self.goodsList;
                for (var i = 0; i < goods.length; i++) {
                    if (goods[i].checked == true) {
                        self.productData.push(goods[i]);
                    }
                }
                if (self.productData.length == 0) {
                    app.toast('没有选择的服务单');
                } else {
                    app.openPage({
                        src: 'fwzp/fwpg-info.html',
                        argsKey: 'fwzp-info',
                        args: {
                            items: self.productData,
                            cb: function () {
                                //刷新数据
                                self._refresh();
                            }
                        }
                    });
                }

            });
        }
    });

    return new fwzp();

});